package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessJPMorgan {
    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("J.P. Morgan Securities Inc.");
                    
                    reports.add(report);
                }
            }

            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        Pattern patternYear = Pattern.compile("JPMORGAN_([0-9]{4})");
        Matcher matcher = patternYear.matcher(report.getFileName());
        if (matcher.find()) {
            int year = Integer.parseInt(matcher.group(1));

            Pattern patternVersion = Pattern.compile("file\\s([0-9]+)[_]{0,1}");
            matcher = patternVersion.matcher(report.getFileName());
            if (matcher.find()) {
                int version = Integer.parseInt(matcher.group(1));
//                getAnalysisReportDetail2003_2004(report);
                //根据year和version调用不同的解析方法
                if(year <= 2004){
                    getAnalysisReportDetail2003_2004(report);
                }
                else if(year <= 2007){
                    if( year == 2005 && version > 3)
                        getAnalysisReportDetail2003_2004(report);
//                    else if(year == 2005 && version == 2)
//                        getAnalysisReportDetail2005_2(report);
                    else if(year == 2005 && version == 1)
                        getAnalysisReportDetail2005_2008(report);
                    else
                        getAnalysisReportDetail2005_2008_version2(report);
                }
                else if(year <= 2011){
                    if(year == 2008 && version > 2)
                        getAnalysisReportDetail2005_2008_version2(report);
                    else if(year == 2011 && version < 2)
                        getAnalysisReportDetail2012(report);
                    else
                        getAnalysisReportDetail2009_2011(report);
                }
                else{
                    getAnalysisReportDetail2012(report);
                }
            }
        }
    }

    public static void getAnalysisReportDetail2003_2004(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Analyst name
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherEmail = patternEmail.matcher(line);
            if(matcherEmail.find()) {
                String analystLine = lines[i - 2].trim();
                if(lines[i-1].trim().contains("[Times-Bold]<7.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("[Times-Bold]<7.0>") + 17);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }else {
                    if(analystLine.contains("[Times-Bold]<7.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("[Times-Bold]<7.0>") + 17));
                    else
                        report.addAnalystName(analystLine);
                }
            }
        }

        //Company name, Rating
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("[Times-BoldItalic]<12.0>") && line.contains("[Times-Bold]<12.0>")){
                int begin1 = line.indexOf("<12.0>");
                int end1 = line.indexOf("[Times-Bold]");
                int begin2 = line.lastIndexOf("<12.0>");
                report.setCompanyName(line.substring(begin1+6,end1));
                report.setRating(line.substring(begin2+6));

                break;
            }
        }

        //InitiatingCoverage
        for(int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if (line.contains("[Helvetica-Bold]<19.0>")) {
                if(line.contains("InitiatingCoverage")){
                    report.setInitiatingCoverage("Yes");
                }
                else{
                    report.setInitiatingCoverage("No");
                }
                break;
            }
        }

        //Ticker
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("[Times-Italic]<7.0>")){
                int begin = line.indexOf(">");
                int end = line.lastIndexOf("[");
                String value = line.substring(begin+1,end);
                report.setTicker(value.split(" ")[1]);
                break;
            }
        }

        //Analyst Position: Sentence
        content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getEndPage());
        lines = content.split("\r\n");
        String sentence = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Analyst Position:")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        report.setSentenceWithKeyWord(sentence);
        report.setPriorRecommendation("");
        report.setExchange("");

        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetail2005_2008(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //Date
        int count = 0;
        Pattern patternDate = Pattern.compile("([0-9]+\\s*(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]{4})");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if(matcherDate.find()) {
                if(count == 0){
                    report.setDate(matcherDate.group());
                    count += 1;
                }
                else{
                    String ticker = lines[i+1];
                    i = i + 2;
                    while(!lines[i].contains("•")){
                        ticker += lines[i];
                        i += 1;
                    }
                    report.setTicker(ticker);
                }
            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Analyst name
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherEmail = patternEmail.matcher(line);
            if(matcherEmail.find()) {
                String analystLine = lines[i - 2].trim();
                if(lines[i-1].trim().contains("[Arial,Bold]<9.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<9.0>") + 17);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }else if(lines[i-1].trim().contains("[Arial,Bold]<8.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<8.0>") + 17);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }
                else {
                    if(analystLine.contains("[Arial,Bold]<9.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<9.0>") + 17));
                    else if(analystLine.contains("[Arial,Bold]<8.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<8.0>") + 17));
                    else
                        report.addAnalystName(analystLine);
                }
            }
        }

        //Company name, Rating
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("[Arial,Bold]<19.0>")){
                int mediate = 0;
                if(line.contains("Neutral"))
                    mediate = line.indexOf("Neutral");
                else if(line.contains("Overweight"))
                    mediate = line.indexOf("Overweight");
                else if(line.contains("Underweight"))
                    mediate = line.indexOf("Underweight");
                if(mediate != 0){
                    report.setCompanyName(line.substring(18,mediate));
                    String temp = line.substring(mediate);
                    int end = temp.indexOf("[");
                    if(end != -1)
                        report.setRating(temp.substring(0,end));
                    else
                        report.setRating(temp);
                }
                else{
                    report.setCompanyName(line);
                    report.setRating(line);
                }
            }
        }

        //InitiatingCoverage
        report.setInitiatingCoverage("No");

        //Analyst Position: Sentence
        content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getEndPage());
        lines = content.split("\r\n");
        String sentence = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Analyst Position:")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        report.setSentenceWithKeyWord(sentence);
        report.setPriorRecommendation("");
        report.setExchange("");

        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetail2005_2008_version2(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //Date
        int count = 0;
        Pattern patternDate = Pattern.compile("([0-9]+\\s*(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]{4})");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if(matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Ticker
        Pattern patternTicker = Pattern.compile(".*\\(.*;.*\\)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherTicker = patternTicker.matcher(line);
            if (matcherTicker.find()) {
                System.out.println(String.format("The line content is %s", line));
                String ticker = "";
                int begin = line.indexOf("(");
                int end = line.indexOf(")");
                String temp = line.substring(begin + 1,end);
                ticker = temp.split(";")[0];
                report.setTicker(ticker);
            }
        }

        if(report.getTicker().equals("")){
            int count2 = 0;
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate.matcher(line);
                if(matcherDate.find()) {
                    if(count2 == 0){
                        count2 += 1;
                    }
                    else{
                        report.setDate(matcherDate.group());
                        String ticker = lines[i+1];
                        i = i + 2;
                        while(!lines[i].contains("•")){
                            ticker += lines[i];
                            i += 1;
                        }
                        report.setTicker(ticker);
                    }
                }
            }
        }

        content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Analyst name
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherEmail = patternEmail.matcher(line);
            if(matcherEmail.find()) {
                String analystLine = lines[i - 2].trim();
                if(lines[i-1].trim().contains("[Arial,Bold]<9.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<9.0>") + 17);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }else if(lines[i-1].trim().contains("[Arial,Bold]<8.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<8.0>") + 17);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }
                else {
                    if(analystLine.contains("[Arial,Bold]<9.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<9.0>") + 17));
                    else if(analystLine.contains("[Arial,Bold]<8.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("[Arial,Bold]<8.0>") + 17));
                    else
                        report.addAnalystName(analystLine);
                }
            }
        }

        //Company name, Rating
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("[Arial,Bold]<19.0>")){
                int mediate = 0;
                if(line.contains("Neutral"))
                    mediate = line.indexOf("Neutral");
                else if(line.contains("Overweight"))
                    mediate = line.indexOf("Overweight");
                else if(line.contains("Underweight"))
                    mediate = line.indexOf("Underweight");
                if(mediate != 0){
                    report.setCompanyName(line.substring(18,mediate));
                    String temp = line.substring(mediate);
                    int end = temp.indexOf("[");
                    if(end != -1)
                        report.setRating(temp.substring(0,end));
                    else
                        report.setRating(temp);
                }
                else{
                    report.setCompanyName(line);
                    report.setRating(line);
                }
            }
        }

        //InitiatingCoverage
        report.setInitiatingCoverage("No");

        //Analyst Position: Sentence
        content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getEndPage());
        lines = content.split("\r\n");
        String sentence = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Analyst Position:")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        report.setSentenceWithKeyWord(sentence);
        report.setPriorRecommendation("");
        report.setExchange("");

        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetail2009_2011(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //Broker name
//        report.setBrokerName("North America Equity Research");

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //Date
        Pattern patternDate = Pattern.compile("([0-9]+\\s*(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]{4})");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Ticker
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if (line.contains("Price:")) {
                //Set ticker
                String ticker = "";
                if(lines[i-1].contains("Arial,Bold]<7.0>")){
                    ticker = lines[i-1].substring(lines[i-1].lastIndexOf("Arial,Bold]<7.0>")+16);
                }
                report.setTicker(ticker.split(",")[0]);

            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Analyst name
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherEmail = patternEmail.matcher(line);
            if(matcherEmail.find()) {
                String analystLine = lines[i - 2].trim();
                if(lines[i-1].trim().contains("Arial,Bold]<9.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("Arial,Bold]<9.0>") + 16);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }else if(lines[i-1].trim().contains("Arial,Bold]<8.0>")){
                    analystLine = lines[i-1].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("Arial,Bold]<8.0>") + 16);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }
                else {
                    if(analystLine.contains("Arial,Bold]<9.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("Arial,Bold]<9.0>") + 16));
                    else if(analystLine.contains("Arial,Bold]<8.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("Arial,Bold]<8.0>") + 16));
                    else
                        report.addAnalystName(analystLine);
                }
            }
        }

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Company name, Rating
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Arial,Bold]<19.0>")){
                int begin = line.indexOf("Arial,Bold]<19.0>");
                int mediate = 0;
                if(line.contains("Arial,Bold]<13.0>"))
                    mediate = line.indexOf("Arial,Bold]<13.0>");
                if(mediate != 0){
                    report.setCompanyName(line.substring(begin + 17,mediate-1));
                    String temp = line.substring(mediate + 17);
                    int end = temp.indexOf("[");
                    if(end != -1)
                        report.setRating(temp.substring(0,end));
                    else
                        report.setRating(temp);
                }
                else{
                    int begin1 = line.indexOf("Arial,Bold]<19.0>");
                    String temp2 = line.substring(begin1 + 17);
                    int end = temp2.indexOf("[");
                    if(end != -1){
                        report.setCompanyName(temp2.substring(0,end));
                    }
                    else{
                        report.setCompanyName(temp2);
                    }
                    report.setRating(line);
                }
            }
        }

        //InitiatingCoverage
        report.setInitiatingCoverage("No");

        //Analyst Position: Sentence
        content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getEndPage());
        lines = content.split("\r\n");
        String sentence = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Analyst Position:")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        report.setSentenceWithKeyWord(sentence);
        report.setPriorRecommendation("");
        report.setExchange("");

        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetail2012(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        //Date
        Pattern patternDate = Pattern.compile("([0-9]+\\s*(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]{4})");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Ticker
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if (line.contains("Price:")) {
                //Set ticker
                String ticker = "";
                if(lines[i-1].contains("Arial,Bold]<7.0>")){
                    ticker = lines[i-1].substring(lines[i-1].lastIndexOf("Arial,Bold]<7.0>")+16);
                }
                report.setTicker(ticker.split(",")[0]);

            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        content = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Analyst name
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherEmail = patternEmail.matcher(line);
            if(matcherEmail.find()) {
                String analystLine = lines[i - 3].trim();
                if(lines[i-2].trim().contains("Arial,Bold]<8.0>")){
                    analystLine = lines[i-2].trim();
                    String mediate = analystLine.substring(analystLine.lastIndexOf("Arial,Bold]<8.0>") + 16);
                    int end = mediate.indexOf("[");
                    if(end != -1)
                        report.addAnalystName(mediate.substring(0,end));
                    else
                        report.addAnalystName(mediate);
                }
                else {
                    if(analystLine.contains("Arial,Bold]<8.0>"))
                        report.addAnalystName(analystLine.substring(analystLine.lastIndexOf("Arial,Bold]<8.0>") + 16));
                    else
                        report.addAnalystName(analystLine);
                }
            }
        }

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(), report.getStartPage(), report.getStartPage());
        lines = content.split("\r\n");
        //Company name, Rating
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Arial,Bold]<19.0>")){
                int begin = line.indexOf("Arial,Bold]<19.0>");
                int mediate = 0;
                if(line.contains("Arial,Bold]<13.0>"))
                    mediate = line.indexOf("Arial,Bold]<13.0>");
                if(mediate != 0){
                    report.setCompanyName(line.substring(begin + 17,mediate-1));
                }
                else{
                    int begin1 = line.indexOf("Arial,Bold]<19.0>");
                    String temp2 = line.substring(begin1 + 17);
                    int end = temp2.indexOf("[");
                    if(end != -1){
                        report.setCompanyName(temp2.substring(0,end));
                    }
                    else{
                        report.setCompanyName(temp2);
                    }
                }
            }
        }

        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            Pattern patternRating = Pattern.compile(".*Bold]<13.0>Neutral|Overweight|Underweight.*");
            Matcher matcherRating = patternRating.matcher(line);
            if(matcherRating.find()) {
                String temp = matcherRating.group();
                int begin = -1;
                if(temp.contains(">"))
                    begin = temp.indexOf(">");
                if(begin != -1)
                    temp = temp.substring(begin + 1);
                report.setRating(temp.split(" ")[0]);
            }
        }

        //InitiatingCoverage
        report.setInitiatingCoverage("No");

        //Analyst Position: Sentence
        content = FileUtil.pdfReader(report.getFilePath(),report.getStartPage(),report.getEndPage());
        lines = content.split("\r\n");
        String sentence = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("Analyst Position:")){
                sentence = line;
                i = i + 1;
                while(i < lines.length ){
                    sentence += lines[i];
                    if(lines[i].contains(".")){
                        break;
                    }else{
                        i++;
                    }
                }
                break;
            }
        }
        report.setSentenceWithKeyWord(sentence);
        report.setPriorRecommendation("");
        report.setExchange("");

        System.out.println(JSON.toJSON(report));
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] Files = new File(rootDir).listFiles();
        for (File file : Files) {
            if(file.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(file.getPath()));
        }

        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Exchange");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        headers.add("Ownership disclosure");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getExchange(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation(),
                    report.getSentenceWithKeyWord(),
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + ""};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
